
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Xml;
using Swaf.BizObj;

namespace Swaf.BizObj.xml
{
	/// <summary>
	/// Implements the abstract methods of <c>BusinessObjectBase</c> and exposes one
	/// new abstract method <c>xmlElement</c> that this class uses as its data store
	/// for the properties of the given business object.</summary>
	[Serializable]
	public abstract class XmlBaseBusinessObject:BaseBizObj
	{
		/// <summary> Child classes should implement this method to provide access to 
		/// the XML element that this object is associated with.  Many of the 
		/// other methods in this class use this method to obtain the element in order
		/// to work with the base data for the business object.
		/// </summary>
		protected internal abstract XmlElement xmlElement { get;}

		protected override BitFlags DirtyFlags
		{get{
			 if(xmlElement == null) return new BitFlags();
			string df = xmlElement.GetAttribute("_dirty");
			if (df == null || df.Length == 0)
				return new BitFlags();
			return new BitFlags(df);
		}}

		public override bool isNew
		{
				get
		 {
			 if(xmlElement == null) return false;
			 string df = xmlElement.GetAttribute("_new");
			 if (df == null || df.Length == 0)
				 return false;
			 try
			 {
				 return bool.Parse(df);
			 }
			 catch (Exception)
			 {
			 }
			 return false;
		 }set
		  {
			  if(xmlElement != null)
				  xmlElement.SetAttribute("_new", System.Convert.ToString(value));
		  }
		}

		/// <summary>
		/// Checks if <code>this</code> <code>BusinessObject</code> has been flagged for logical deletion.  </summary>
		/// <returns>
		public override bool isDeleted
		{
			get
			{
				if(xmlElement == null) return false;
				string df = xmlElement.GetAttribute("_delete");
				if (df == null || df.Length == 0)
					return false;
				try
				{
					return bool.Parse(df);
				}
				catch (Exception)
				{
				}
				return false;
			}set
			 {
				 if(xmlElement != null)
					 xmlElement.SetAttribute("_delete", System.Convert.ToString(value));
			 }
		}

		protected override BitFlags ValidatedFlags
		{get{
			 if(xmlElement == null) return new BitFlags();
			string vf = xmlElement.GetAttribute("_validated");
			if (vf == null || vf.Length == 0)
				return new BitFlags();
			return new BitFlags(vf);
		}}
		
		/// <summary>Creates a new instance of XmlBaseBusinessObject</summary>
		public XmlBaseBusinessObject(IBizObjDefinition info)
			:base(info)
		{
		}
				
		public override object Clone()
		{
			return new XmlBusinessObject(m_bizObjType, xmlElement);
		}

		protected override void removeValue(string fieldName)
		{
			throw new Exception("The method or operation is not implemented.");
		}

		protected override object getSingleName(string singleName)
		{
			throw new Exception("The method or operation is not implemented.");
		}

		protected override object getSingleName(string singleName, object[] extra)
		{
			throw new Exception("The method or operation is not implemented.");
		}

		protected override void putSingleName(string singleName, object val, object extraInfo, bool shouldValueBeFieldType)
		{
			throw new Exception("The method or operation is not implemented.");
		}

		protected override object createPlatformTypeInstance(BizObjField field, object val)
		{
			throw new Exception("The method or operation is not implemented.");
		}

		public override bool containsSingleField(string fieldName)
		{
			throw new Exception("The method or operation is not implemented.");
		}

        protected override bool isSingleFieldSet(string singleName)
        {
            throw new Exception("The method or operation is not implemented.");
        }
	}
}